<script type="text/html" id="superiorlist">
    {{each list as item index}}
    <div class="form-group" style=" margin-left:0px;">
        <div class="col-sm-8">
            <a href="{:_url('sys_admin.users/info',['user_id'=>'[[item.user_id]]'])}" data-toggle="wrapperAjax"> {{item.level}}级 - 会员ID:{{item.user_id}} - 姓名：{{item.real_name}} - 代理层级：{{item.proxy_name}}</a>
        </div>
    </div>
    {{/each}}
</script>
<script type="text/html" id="optloglist">
    {{each list as item index}}
    <tr>
        <td>{{item.log_id}}{{v}}</td>
        <td>{{item.admin_info}}</td>
        <td>{{item.log_time}}</td>
        <td>{{item.log_info}}</td>
    </tr>
    {{/each}}
</script>
{literal}
<script type="text/html" id="regionProxyBox">
    {{if region_proxy == 1}}
    <span class="btn btn-sm btn-default m-r m-b-md regionProxyDel">
        <input type="hidden" name="proxy_region_id[]" value="{{province_id}}">{{province_text}}<i class="fa fa-times m-l"></i>
    </span>
    {{/if}}
    {{if region_proxy == 2}}
    <span class="btn btn-sm btn-default m-r m-b-md regionProxyDel">
        <input type="hidden" name="proxy_region_id[]" value="{{city_id}}">{{province_text}},{{city_text}}<i class="fa fa-times m-l"></i>
    </span>
    {{/if}}
    {{if region_proxy == 3}}
    <span class="btn btn-sm btn-default m-r m-b-md regionProxyDel">
        <input type="hidden" name="proxy_region_id[]" value="{{district_id}}">{{province_text}},{{city_text}},{{district_text}}<i class="fa fa-times m-l"></i>
    </span>
    {{/if}}
</script>
{/literal}
<script type="text/javascript">
    $(document).on('click','.superior_tab',function(){
        var _user_id = $(this).data('user_id');
        if ($('#tabSuperior_'+_user_id).find('.table-responsive').html() == ''){
            getSuperiorList(_user_id);
        }
    })
    $(document).on('click','.chain_tab',function(){
        var _user_id = $(this).data('user_id');
        if ($('#team_chain_user_info').html() == ''){
            $('#MyTree2').tree({
                dataSource: new DataSourceTree({
                    data: getTeamList(_user_id)
                }),
                multiSelect: true,
                iconSelect:true,
                loadingHTML: '<div class="tree-loading"><i class="fa fa-rotate-right fa-spin"></i></div>'
            });
        }
    })
    $(document).on('click','.optlog_tab',function(){
        var _user_id = $(this).data('user_id');
        if ($('#tabOptLogBox_'+_user_id).find('tbody').html() == ''){
            getOptLogList(_user_id);
        }
    })


    $(document).on('click','.next_btn',function(){
        getChainList($(this).data('id'),true)
    })
    var selRegionProxy = 0;
    //选择代理区域
    function selProxyRegion(){
        var region_proxy = $('input[name="region_proxy"]:checked').val();
        if (region_proxy == selRegionProxy) return false;
        $('.regionProxyList').html('');
        selRegionProxy = region_proxy;
    }
    //区域代理授权
    function addRegion(){
        var data = new Object();
        data.region_proxy = $('input[name="region_proxy"]:checked').val();
        if (data.region_proxy > 0){
            data.province_id = $('select[name="province"]').val();
            data.province_text = $('select[name="province"]').find("option:selected").text();
        }
        if (data.region_proxy == 1 && data.province_id < 1){
            return _alert('选择代理的省份.');
        }
        if (data.region_proxy > 1){
            data.city_id = $('select[name="city"]').val();
            data.city_text = $('select[name="city"]').find("option:selected").text();
        }
        if (data.region_proxy == 2 && data.city_id < 1){
            return _alert('选择代理的城市.');
        }
        if (data.region_proxy > 2){
            data.district_id = $('select[name="district"]').val();
            data.district_text = $('select[name="district"]').find("option:selected").text();
        }
        if (data.region_proxy == 3 && data.district_id < 1){
            return _alert('选择代理的区域.');
        }
        var isOk = true;
        $('.regionProxyList').find('input').each(function(){
            var val = $(this).val();
            if (val == data.province_id || val == data.city_id || val == data.district_id){
                isOk = false;
                return _alert('选中的已存在.');
            }
        })
        if (isOk == false) return false;
        $('.regionProxyList').append(template('regionProxyBox', data));
    }
    $(document).on('click','.regionProxyDel',function(){
        $(this).remove();
    })
    //区域代理授权

    //加载上级
    function getSuperiorList(uid){
        jq_ajax('{:url("sys_admin.users/getSuperiorList")}','user_id='+uid,function(res){
            if (res.list.length < 1){
                $('#tabSuperior_'+uid).find('.table-responsive').html('<div class="m-b">厂家</div>');
                return false;
            }
            $('#tabSuperior_'+uid).find('.table-responsive').html(template('superiorlist',res));
        });
    }

    //加载操作日志
    function getOptLogList(uid){
        jq_ajax('{:url("sys_admin.users/getOptLogList")}','user_id='+uid,function(res){
            if (res.data.list.length > 0) {
                console.log(typeof(res.data));
                console.log(res.data);
                $('#tabOptLogBox_'+uid).find('tbody').html(template('optloglist', res.data));
            } else {
                $('#tabOptLogBox_'+uid).find('tbody').html('<tr><td colspan="10" align="center" style="height:150px;">暂无相关日志.</td></tr>')
            }
        });
    }
    //搜索代理
    function searchProxyUser(keyword,selects) {
        var user_keyword = $("#"+keyword).val();
        $("#"+selects+" option").each(function(){
            if ($(this).val() != 0) $(this).remove();
        });
        var res = jq_ajax('{:url("searchUser")}', 'keyword='+user_keyword);
        if (res.msg)  _alert(res.msg);
        if (res.code == 0) return false;
        $.each(res.list,function(i,val){
            $("#"+selects).append("<option value='"+val.user_id+"'>"+val.user_id+'-'+val.mobile+'-'+val.real_name+'-'+val.proxy_name+"</option>");
        });

    }
</script>